 <?php

/**
 * --------------------------------
 * Default ClassesController
 *
 * 班级管理的统一类  ClassesController
 * 
 * --------------------------------
 */

class ClassesController extends BaseController{
	// /**
 //     * 构造函数
 //     * 统一用户Session验证
 //     * 
 //     */
 //    public function __construct(){
 //    	$check_result = Basecontroller::session('internal');
 //    	if(!$check_result) 
 //    		exit();
 //    }

    public function index(){

    	$column = Input::get('column');
        $content = Input::get('content');
        $classes_result = DB::table('classes');
        if(null==$content || null== $column){
			$column ='';
			$content='';
		}
		if(null!=$column && ''!= $column && null!= $content && ''!= $content){

			$classes_result = $classes_result->where('classes.'.$column, 'like', '%'.$content.'%');
        }

		$classes_result = $classes_result
							->select('id','grade','class','student_num','remark')
							->where('is_del',0)
							->paginate(15)
							->toJson();
		$classes_result = json_decode($classes_result);
		foreach ($classes_result->data as $key => $value) {
			$master_tea_name = DB::table('teacher')->select('classes_id','name','tel')->where('classes_id','!=',0)->get();
			foreach ($master_tea_name as $key1 => $value1) {
				if($value->id == $value1->classes_id ){
					$classes_result->data[$key]->master_name = $value1->name;
					$classes_result->data[$key]->tel = $value1->tel;
				}	
			}
			
		}				
		return Response::json(['error'=>false,'resule'=>$classes_result],200);
    }

    /**
	 * 查询单个信息班级接口 
	 * @param  $id
	 * @return Response
	 */
    public function show($id){
        $array = array();
		$array = DB::table('classes')
				->join('teacher','teacher.classes_id','=','classes.id')
				->select('classes.id','classes.grade','classes.class',
						 'classes.student_num','classes.remark',
						 'teacher.name as master_name','teacher.tel')
				->where('classes.id',$id)
				->get();
		foreach ($array as $key => $value) {
			$array[$key]->teacher_data = DB::table('teacher_classes')
						->join('teacher','teacher_classes.teacher_id','=','teacher.id')
						->join('subject','teacher.subject_id','=','subject.id')
						->select('teacher.name as teacher_name','subject.name as subject_name')
						->where('teacher_classes.classes_id',$id)
						->get();			
		}	
		
		                   
		return Response::json(['error'=>false,'result'=>$array],200);	
    }
    

    /**
	 * 新增班级接口 
	 * @param 
	 * @return Response
	 */
	public function store(){


		if(!Input::has('grade') || !Input::has('class'))
			return Response::json(['error'=>true,'message'=>'缺少重要信息'],201);

		$master_id = Input::get('teacher_id');
		$exist_master_id = DB::table('teacher')->select('id','name')->where('id',$master_id)
											   ->where('classes_id',"=",0)->get();
		if($exist_master_id)
			return Response::json(['error'=>true,'message'=>'该教师已是班主任'],201);
		

		//开启事务
		DB::beginTransaction();
	 	$new_classes_arr = array();
		$message = Input::all();
		$classes_message = array('grade','class','remark');
		foreach ($message as $key => $value) {
			if(in_array($key,$classes_message)){
				$new_classes_arr[$key] = $value;
			}
		}
		$new_classes_id = DB::table('classes')->insertGetId($new_classes_arr);
	 	if(!$new_classes_id){
	 		DB::rollback();
	 	}
	 	//给新班级分配班主任
	 	$new_teacher_result = DB::table('teacher')
								->where('id',$master_id)
								->update(array('classes_id'=>$new_classes_id));
		if(!$new_teacher_result){
	 		DB::rollback();
	 	}

	 	//新增班级教师
	 	$teacher_id = array();	
 		$teacher_id = Input::get('subject_teacher_id');
 		$insert_arr = array();
 		foreach ($teacher_id as $key => $value) {
 			$insert_arr[] = array(
 				'teacher_id'=>$value,
 				'classes_id'=>$new_classes_id,
 				);
 		}
 		$result = DB::table('teacher_classes')->insert($insert_arr);
 		if(!$result){
	 		DB::rollback();
	 	}
	 	DB::commit();
 		return Response::json(['error'=>false,'message'=>'创建成功','result'=>$new_classes_id],200);	
	 	
 	}

	/**
	 * 删除班级接口 
	 * @param 
	 * @return Response
	 */
	public function destroy($id){

	    $del_result = DB::table('classes')->where('id',$id)->update(array('is_del'=>1));
	    if($del_result){
	    	return Response::json(['error'=>false,'message'=>'删除成功'],200);
	    }else{
	    	return Response::json(['error'=>true,'message'=>'删除失败'],201);
	    }

	}
}